package com.dd.shop4j.website.goods.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dd.shop4j.common.utils.enums.OrderByType;
import com.dd.shop4j.goods.mapper.GoodsBrandMapper;
import com.dd.shop4j.goods.mapper.GoodsDescriptionMapper;
import com.dd.shop4j.goods.mapper.GoodsImageMapper;
import com.dd.shop4j.goods.mapper.GoodsInfoMapper;
import com.dd.shop4j.goods.model.GoodsBrand;
import com.dd.shop4j.goods.model.GoodsDescription;
import com.dd.shop4j.goods.model.GoodsImage;
import com.dd.shop4j.goods.model.GoodsInfo;
import com.dd.shop4j.website.controller.forms.GoodsSearchForm;
import com.dd.shop4j.website.controller.forms.PriceRange;

@Service
@Transactional(rollbackFor = Exception.class)
public class GoodsInfoService {
	
	@Resource private GoodsBrandMapper goodsBrandMapper;
	@Resource private GoodsInfoMapper goodsInfoMapper;
	@Resource private GoodsDescriptionMapper goodsDescriptionMapper;
	@Resource private GoodsImageMapper goodsImageMapper;
	
	public List<GoodsInfo> getGoodsInfoByPage(GoodsInfo good, int start, int limit, String orderby, OrderByType orderByType) {
		//如果为空，设置为默认排序
		if (orderByType == null) {
			orderByType = OrderByType.DESC;
		}
		return goodsInfoMapper.queryPageBySelective(good, start, limit, "add_time", orderByType.getCode());
	}
	
	public List<PriceRange> getPriceRange(GoodsSearchForm form) {
		Integer range = goodsInfoMapper.getAvgRange();
		if (range == null || range == 0) {
			return null;
		}
		String rangeStr = "1";
		for (int i = 0; i < range.toString().length(); i++) {
			rangeStr += "0";
		}
		range = Integer.parseInt(rangeStr);
		List<Map<String, Object>> rs = goodsInfoMapper.groupByPrice(range);
		int count = 0;
		for (Map<String, Object> r : rs) {
			count += ((Long) r.get("c")).intValue();
		}
		List<PriceRange> result = new ArrayList<PriceRange>();
		int c = count / 5;
		while (CollectionUtils.isNotEmpty(rs)) {
			PriceRange pr = new PriceRange();
			pr.setPriceBegin(((Double)rs.get(0).get("p")).intValue() * range);
			int temp = ((Long) rs.get(0).get("c")).intValue();
			rs.remove(0);
			List<Map<String, Object>> removeList = new ArrayList<Map<String,Object>>();
			for (Map<String, Object> r : rs) {
				temp += ((Long) r.get("c")).intValue();
				if (temp >= c) {
					pr.setPriceEnd((((Double)r.get("p")).intValue() + 1) * range - 1);
					removeList.add(r);
					break;
				}
			}
			rs.removeAll(removeList);
			if (pr.getPriceEnd() != null && pr.getPriceEnd() > 0) {
				pr.setText(pr.getPriceBegin() + " - " + pr.getPriceEnd());
			} else {
				pr.setText(pr.getPriceBegin() + "以上");
			}
			result.add(pr);
		}
		return result;
	}
	
	public GoodsInfo getGoodsInfo(Integer goodId) {
		return goodsInfoMapper.selectByPrimaryKey(goodId);
	}
	
	public GoodsDescription getGoodsDescription(Integer goodId){
		return goodsDescriptionMapper.selectByPrimaryKey(goodId);
	}
	
	public GoodsBrand getGoodsBrandById(Integer brandId) {
		return goodsBrandMapper.selectByPrimaryKey(brandId);
	}

	public List<GoodsImage> getGoodsImagesByGoodId(Integer goodId) {
		GoodsImage query = new GoodsImage();
		query.setGoodId(goodId);
		return goodsImageMapper.queryBySelective(query);
	}
}
